package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AdminDAO;
import dao.ArticleDAO;
import dao.UserDAO;
import entity.Admin;
import entity.User;

/**
 * Servlet implementation class JudgeUserServlet
 */
@WebServlet("/JudgeUserServlet")
public class JudgeUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JudgeUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String type = request.getParameter("type");
		int userID = Integer.parseInt(request.getParameter("userID"));
		if(type.equals("0")) {//type=0，为删除user操作
			//将该用户发布的帖子的发帖子设置为“已注销用户”
			ArticleDAO.getInstance().modifyUser_toDefault(userID);
			UserDAO.getInstance().delete(userID);
			RequestDispatcher view = request.getRequestDispatcher("ManageUserServlet");
			view.forward(request, response);
		}else if(type.equals("1")){
			//编辑user信息
            User user = UserDAO.getInstance().get(userID);
			request.setAttribute("modify_user_id", user.getUser_id());
			request.setAttribute("modify_user_name", user.getUser_name());
			request.setAttribute("modify_user_password", user.getUser_password());
			
			String sex="男";
			if(user.getUser_sex()==0) sex="女";
			String islocked = "禁用";
			if(user.getUser_islocked()==0) islocked="正常";
			request.setAttribute("modify_user_sex", sex);
			request.setAttribute("modify_user_islocked", islocked);
			request.setAttribute("modify_user_email", user.getUser_email());
			request.setAttribute("modify_user_tel", user.getUser_tel());
			RequestDispatcher view = request.getRequestDispatcher("user_Modify.jsp");
			view.forward(request, response);
		}else {
			//对用户进行加解锁
			User user = UserDAO.getInstance().get(userID);
			int islocked = Integer.parseInt(request.getParameter("operation"));
			if(islocked==0) UserDAO.getInstance().lockUser(user);
			else {
				 UserDAO.getInstance().cancelLockUser(user);
			}
			RequestDispatcher view = request.getRequestDispatcher("ManageUserServlet");
			view.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
